package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.GroupActivityDao;
import com.ytjj.qmyx.admin.model.BaseGroupInfo;
import com.ytjj.qmyx.admin.model.request.GroupActivityReq;
import com.ytjj.qmyx.admin.model.request.goods.GoodsRequest;
import com.ytjj.qmyx.admin.model.vo.GroupActivityVO;
import com.ytjj.qmyx.admin.model.vo.ProductVO;
import com.ytjj.qmyx.admin.service.GroupActivityService;
import com.ytjj.qmyx.admin.service.MemberService;
import com.ytjj.qmyx.mall.mapper.ProductCategoryMapper;
import com.ytjj.qmyx.mall.model.ProductCategory;
import com.ytjj.qmyx.mall.model.ProductCategoryExample;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
@AllArgsConstructor
public class GroupActivityServiceImpl implements GroupActivityService {

    private GroupActivityDao groupActivityDao;

    private MemberService memberService;

    private ProductCategoryMapper productCategoryMapper;

    @Override
    public CommonPage<GroupActivityVO> selectGroupList(GroupActivityReq groupActivityReq) {
        PageHelper.startPage(groupActivityReq.getPageNum(), groupActivityReq.getPageSize());
        List<GroupActivityVO> groupSelectVOList = groupActivityDao.selectGroupList(groupActivityReq);
        //拼团中的商品id
        List<Integer> productIds = groupActivityDao.selectIdsByFinalResult();
        if (!CollectionUtils.isEmpty(groupSelectVOList)) {
            List<ProductCategory> productCategoryAllList = productCategoryMapper.selectByExample(new ProductCategoryExample());
            for (GroupActivityVO groupActivityVO : groupSelectVOList) {
                // 产品分类
                productCategoryAllList.stream().forEach(s -> {
                    if (null != groupActivityVO.getTopCategoryId() && groupActivityVO.getTopCategoryId().equals(s.getId())) {
                        groupActivityVO.setCategoryTopTitle(s.getTitle());
                    }
                    if (null != groupActivityVO.getCategoryId() && groupActivityVO.getCategoryId().equals(s.getId())) {
                        groupActivityVO.setCategoryTitle(s.getTitle());
                    }
                    if (null != groupActivityVO.getBottomCategoryId() && groupActivityVO.getBottomCategoryId().equals(s.getId())) {
                        groupActivityVO.setCategoryBottomTitle(s.getTitle());
                    }
                });
                List<BaseGroupInfo> baseGroupInfos = groupActivityDao.selectNumber(groupActivityVO.getId());
                groupActivityVO.setCurrentList(baseGroupInfos);
                //判断是否在拼团并返回
                AtomicReference<Boolean> b = new AtomicReference<>(false);
                if (!CollectionUtils.isEmpty(productIds)){
                    productIds.stream().forEach(productId ->{
                        if (productId.equals(groupActivityVO.getProductId())){
                            b.set(true);
                        }
                    });
                }
                groupActivityVO.setIsGroupBooking(b.get());
                //参团人数
                Integer integer = groupActivityDao.selectNum(groupActivityVO.getId());
                groupActivityVO.setJoinNum(null == integer ? 0 : integer);
                //判断是否按比例分成
                if (null != groupActivityVO.getDivideWay() && 1 == groupActivityVO.getDivideWay()){
                    BigDecimal groupPrice = groupActivityVO.getGroupPrice();//拼团价
                    BigDecimal costPrice = groupActivityVO.getCostPrice();//成本价
                    BigDecimal ratio = new BigDecimal(groupActivityVO.getRatio());//分成比例
                    BigDecimal b1 = new BigDecimal(100);
                    BigDecimal ratioPrice = (groupPrice.subtract(costPrice)).multiply(ratio).divide(b1,2, BigDecimal.ROUND_CEILING);
                    groupActivityVO.setRatioPrice(ratioPrice);
                }
            }
        }
        return CommonPage.restPage(groupSelectVOList);
    }
    @Override
    public boolean insertGroup(BaseGroupInfo baseGroupInfo) {
        baseGroupInfo.setCreateUser(memberService.getAdmin().getId());
        return groupActivityDao.insertGroup(baseGroupInfo);
    }

    @Override
    public boolean deleteGroup(Integer id) {
        return groupActivityDao.deleteGroup(id);
    }

    @Override
    public Integer judgeProduct(Integer id) {
        return groupActivityDao.judgeProduct(id);
    }

    @Override
    public ProductVO selectProduct(Integer id) {
        return groupActivityDao.selectProduct(id);
    }

    @Override
    public boolean updateGroup(BaseGroupInfo baseGroupInfo) {
        baseGroupInfo.setCreateUser(memberService.getAdmin().getId());
        return groupActivityDao.updateGroup(baseGroupInfo);
    }

    @Override
    public BaseGroupInfo detailById(Integer id) {
        return groupActivityDao.detailById(id);
    }

    @Override
    public boolean updateProduct(GoodsRequest goodsRequest) {
        return groupActivityDao.updateProduct(goodsRequest);
    }

    @Override
    public boolean updateGroupPrice(Integer id,BigDecimal groupPrice) {
        return groupActivityDao.updateGroupPrice(id,groupPrice);
    }

    @Override
    public String judgeState(Integer productId) {
        return groupActivityDao.judgeState(productId);
    }

    @Override
    public boolean updateState(Integer productId) {
        return groupActivityDao.updateState(productId);
    }

    @Override
    public boolean updateActivity(Integer productId) {
        return groupActivityDao.updateActivity(productId);
    }

    @Override
    public String judgeH5(Integer productId) {
        return groupActivityDao.judgeH5(productId);
    }

}
